<?php
class lib_game_quan_log extends lib_base{
	public $table_name = "qc_game_quan_log";
    public $pk = "id";

    public function add_log($info_id,$user_id,$intro='',$is_zan=1){
		$info =  D('game_quan')->get_info($info_id);
		$arrBase = array(
			'game_id' =>$info['game_id'],
			'quan_id' =>$info_id,
			'user_id' =>$user_id,
			// 'admin_id' =>$admin_id,
			'intro' =>$intro,
			'is_zan' =>$is_zan,
			'status' =>1,
			'add_time'=>date('Y-m-d H:i:s'),
		);
		return $this->add($arrBase);
		
	}

	public function get_logs($info_id){
		$cond = "quan_id='{$info_id}' and user_id>0 and is_zan=1";
		$result = $this->findAll(array($cond),'id asc','user_id');
		if($result){
			$logusers = D('sys_user')->ids($result,'user_id');
			foreach ($result as $v2) {
				if($logusers[$v2['user_id']]['avatarUrl']){
					$v3['user_id'] = $logusers[$v2['user_id']]['id'];
					$v3['nickName'] = $logusers[$v2['user_id']]['nickName'];
					$v3['avatarUrl'] = $logusers[$v2['user_id']]['avatarUrl'];
					$user3[] = $v3;
				}
			}
			return $user3;
		}
	}

	public function get_replays($info_id){
		$cond = "quan_id='{$info_id}' and user_id>0 and is_zan=0";
		$result = $this->findAll(array($cond),'id asc','user_id,to_user_id,intro,add_time');
		if($result){
			$logusers = D('sys_user')->ids($result,'user_id');
			$to_logusers = D('sys_user')->ids($result,'to_user_id');
			foreach ($result as $v2) {
				
				$v3['user_id'] = $v2['user_id'];
				$v3['to_user_id'] = $v2['to_user_id'];
				$v3['intro'] = $v2['intro'];
				$v3['addtime'] = time_tran($v2['add_time']);
				// dump($v2);exit;
				if($logusers[$v2['user_id']]){
					$v3['nickName'] = $logusers[$v2['user_id']]['nickName'];
					$v3['avatarUrl'] = $logusers[$v2['user_id']]['avatarUrl'];
				}
				if($to_logusers[$v2['to_user_id']]){
					$v3['to_nickName'] = $to_logusers[$v2['to_user_id']]['nickName'];
					$v3['to_avatarUrl'] = $to_logusers[$v2['to_user_id']]['avatarUrl'];
				}
				$user3[] = $v3;
			}
			return $user3;
		}
	}

	public function dianzan($info_id,$user_id)
	{
		$cond = "quan_id='{$info_id}' and user_id='{$user_id}' and is_zan=1 and status=1";
		$log = $this->find(array($cond));
		if(!$log){
			$info_id = $this->add_log($info_id,$user_id);
		}else{
			$info_id = $log['id'];
		}

		return $info_id;
	}

	public function pinglun($info_id,$user_id,$to_user_id,$intro='')
	{
		$info =  D('game_quan')->get_info($info_id);
		
		$arrBase = array(
			'game_id' =>$info['game_id'],
			'quan_id' =>$info_id,
			'user_id' =>$user_id,
			'to_user_id' =>$to_user_id,
			'intro' =>$intro,
			'is_zan' =>0,
			'status' =>1,
			'add_time'=>date('Y-m-d H:i:s'),
		);
		
		return $this->add($arrBase);
	}
}